package com.kateba.android.hid;

import android.media.SoundPool;
import android.view.KeyEvent;
import android.view.inputmethod.InputConnection;

import com.kateba.android.blueinput.Singleton;



public class Action
{
  static final int MAX_EK_KEYS = 255;
  private static int m_sndKB;
  private static int m_sndMouse;
  private static SoundPool m_sndSP = null;
  public int DevType;
  public int[] EK_BufKeys = new int['ÿ'];
  public int[] EK_BufUses = new int['ÿ'];
  int[] EK_PrevBufKeys = new int['ÿ'];
  int EK_PrevBufSize = 0;
  int[] EK_PrevBufUses = new int['ÿ'];
  public int[] KB_BufKeys = new int['ÿ'];
  public int KB_BufSize;
  public int KB_Modifier;
  public int KB_Power;
  int[] KB_PrevBufKeys = new int[48];
  int KB_PrevBufSize = 0;
  final String LOG_TAG = "BlueInput::Action";
  int MOUSE_PrevBut = 0;
  public int Mouse_Buts;
  public int Mouse_Wheel;
  public int Mouse_X;
  public int Mouse_Y;
  int OLDSK_ARRAY = 0;
  private boolean g_bAlted = false;
  private boolean g_bControled = false;
  boolean g_bSKEY_Capslock = false;
  boolean g_bSKEY_Numlock = false;
  private boolean g_bShifted = false;
  public com.kateba.android.hid.KBRep poutKBRep = new com.kateba.android.hid.KBRep(this);
private int i;

  static
  {
    m_sndKB = 0;
    m_sndMouse = 0;
  }

  int ChangeKB(int paramInt, boolean paramBoolean1, boolean paramBoolean2)
  {
    int i = 0;
    switch (paramInt)
    {
    default:
      i = paramInt;
    case 84:
    case 85:
    case 86:
    case 87:
    case 88:
    case 89:
    case 90:
    case 91:
    case 92:
    case 93:
    case 94:
    case 95:
    case 96:
    case 97:
    case 98:
    case 99:
    }
//    while (true)
//    {
//     // return i;
//      i = 56;
//      continue;
//      i = 85;
//      continue;
//      i = 86;
//      continue;
//      i = 87;
//      continue;
//      i = 88;
//      continue;
//      if (paramBoolean1)
//      {
//        i = 30;
//        continue;
//      }
//      i = 77;
//      continue;
//      if (paramBoolean1)
//      {
//        i = 31;
//        continue;
//      }
//      i = 81;
//      continue;
//      if (paramBoolean1)
//      {
//        i = 32;
//        continue;
//      }
//      i = 78;
//      continue;
//      if (paramBoolean1)
//      {
//        i = 33;
//        continue;
//      }
//      i = 80;
//      continue;
//      if (paramBoolean1)
//        paramInt = 34;
//      i = paramInt;
//      continue;
//      if (paramBoolean1)
//      {
//        i = 35;
//        continue;
//      }
//      i = 79;
//      continue;
//      if (paramBoolean1)
//      {
//        i = 36;
//        continue;
//      }
//      i = 74;
//      continue;
//      if (paramBoolean1)
//      {
//        i = 37;
//        continue;
//      }
//      i = 82;
//      continue;
//      if (paramBoolean1)
//      {
//        i = 38;
//        continue;
//      }
//      i = 75;
//      continue;
//      if (paramBoolean1)
//      {
//        i = 39;
//        continue;
//      }
//      i = 73;
//      continue;
//      if (paramBoolean1)
//      {
//        i = 55;
//        continue;
//      }
//      i = 76;
//    }
	return i;
  }

  public void DOHIDACTION()
  {
    String str = "";
    if (this.DevType == 0)
      str = "keyboard";
    if (this.DevType == 1)
      str = "mouse";
    if (this.DevType == 2)
      str = "extra_keys";
    if (this.DevType == 3)
      str = "power_state";
    Singleton.getInstance().Debug("BlueInput::Action", "DOHIDACTION type=" + this.DevType + " [" + str + "]");
    UpdateKB(this.KB_Modifier, this.KB_BufKeys, this.KB_BufSize);
    UpdateCursor(this.Mouse_Buts, this.Mouse_X, this.Mouse_Y, this.Mouse_Wheel);
    UpdateEK(this.EK_BufKeys, this.EK_BufUses, 255);
    PowerKB(this.KB_Power);
  }

  void PlaySound(int paramInt)
  {
    if (m_sndSP == null)
    {
      m_sndSP = new SoundPool(2, 3, 0);
      m_sndKB = m_sndSP.load(Singleton.getInstance(), 2130968576, 1);
      m_sndMouse = m_sndSP.load(Singleton.getInstance(), 2130968577, 1);
    }
    if ((paramInt == 0) && (Singleton.getInstance().m_sett.getKBSounds()))
      m_sndSP.play(m_sndKB, 1.0F, 1.0F, 1, 0, 1.0F);
    if ((paramInt == 1) && (Singleton.getInstance().m_sett.getMouseSounds()))
      m_sndSP.play(m_sndMouse, 1.0F, 1.0F, 1, 0, 1.0F);
  }

  void PowerKB(int paramInt)
  {
    Singleton.getInstance().Debug("BlueInput::Action", "PowerKB modipower=" + paramInt);
    if ((paramInt & 0x1) > 0)
    {
      PlaySound(0);
      ToggleNoLayoutKey(26);
    }
    Singleton.getInstance().Debug("BlueInput::Action", "PowerKB done");
  }

  int SendKey(int paramInt1, int paramInt2)
  {
    int i = 0;
    if (Singleton.getInstance().m_vkInst != null)
      try
      {
        int j = KBLayout.GetLayoutKey(paramInt1, this.g_bShifted, this.g_bAlted, this.g_bControled);
        Singleton.getInstance().Debug(0, "BlueInput::Action", "SendKey (0x" + Integer.toHexString(paramInt1) + "|" + paramInt1 + "," + paramInt2 + ") shift:" + this.g_bShifted + " alt:" + this.g_bAlted + " control:" + this.g_bControled + "\n" + "Layout:" + KBLayout.m_nLayoutID + " LayoutKey:" + j);
        if ((KBLayout.m_nLayoutID == 0) || (j == 0))
        {
          SendNoLayoutKey(KBLayout.GetAndroidKey(paramInt1), paramInt2);
//          break label196;
        }
        if (paramInt2 != 0)
//          break label196;
        Singleton.getInstance().m_vkInst.sendKeyChar((char)j);
      }
      catch (Exception localException)
      {
        localException.printStackTrace();
        i = -1;
      }
    else
      i = -1;
    label196: return i;
  }

  int SendNoLayoutKey(int paramInt1, int paramInt2)
  {
    if (Singleton.getInstance().m_vkInst != null);
    while (true)
    {
      try
      {
        InputConnection localInputConnection = Singleton.getInstance().m_vkInst.getCurrentInputConnection();
        if (!this.g_bShifted)
          continue;
        int j = 1;
        localInputConnection.sendKeyEvent(new KeyEvent(0L, 0L, paramInt2, paramInt1, 0, j));
        i = 0;
      //  return i;
        j = 0;
        continue;
      }
      catch (Exception localException)
      {
        localException.printStackTrace();
        i = -1;
        continue;
      }
//       i = -1;
    }
  }

  void ToggleNoLayoutKey(int paramInt)
  {
    SendNoLayoutKey(paramInt, 0);
    SendNoLayoutKey(paramInt, 1);
  }

  void UpdateCursor(int paramInt1, int paramInt2, int paramInt3, int paramInt4)
  {
    Singleton.getInstance().Debug("BlueInput::Action", "UpdateCursor call Buts=" + paramInt1 + " X=" + paramInt2 + " Y=" + paramInt3 + " Wheel=" + paramInt4);
    if (paramInt1 != this.MOUSE_PrevBut)
    {
      if (((0x1 & this.MOUSE_PrevBut) > 0) && ((paramInt1 & 0x1) == 0))
        ToggleNoLayoutKey(66);
      if (((0x2 & this.MOUSE_PrevBut) > 0) && ((paramInt1 & 0x2) == 0))
        ToggleNoLayoutKey(4);
      if (((0x4 & this.MOUSE_PrevBut) > 0) && ((paramInt1 & 0x4) == 0))
        ToggleNoLayoutKey(23);
      if (((0x1 & this.MOUSE_PrevBut) == 0) && ((paramInt1 & 0x1) > 0))
        PlaySound(1);
      if (((0x2 & this.MOUSE_PrevBut) == 0) && ((paramInt1 & 0x2) > 0))
        PlaySound(1);
      if (((0x4 & this.MOUSE_PrevBut) == 0) && ((paramInt1 & 0x4) > 0))
        PlaySound(1);
      this.MOUSE_PrevBut = paramInt1;
    }
    if (paramInt2 < -5)
      ToggleNoLayoutKey(21);
    if (paramInt2 > 5)
      ToggleNoLayoutKey(22);
    if (paramInt3 < -5)
      ToggleNoLayoutKey(19);
    if (paramInt3 > 5)
      ToggleNoLayoutKey(20);
    Singleton.getInstance().Debug("BlueInput::Action", "UpdateCursor done");
  }

  void UpdateEK(int[] paramArrayOfInt1, int[] paramArrayOfInt2, int paramInt)
  {
    Singleton.getInstance().Debug("BlueInput::Action", "UpdateEK call.");
    int i = 0;
    if (i >= paramInt);
    for (int j = 0; ; j++)
    {
      if (j >= paramInt)
      {
        this.EK_PrevBufSize = paramInt;
        Singleton.getInstance().Debug("BlueInput::Action", "UpdateEK done");
//        return;
        if (paramArrayOfInt1[i] != this.EK_PrevBufKeys[i])
        {
          if (paramArrayOfInt1[i] <= 0)
//            break label78;
          PlaySound(0);
        }
        while (true)
        {
          i++;
//          break;
//          label78: 
        	  Singleton.getInstance().Debug("BlueInput::Action", "KEY UP (Page=0x0C) Usage=" + this.EK_PrevBufUses[i]);
          switch (this.EK_PrevBufUses[i])
          {
          case 64:
          case 388:
          case 389:
          case 390:
          case 391:
          case 392:
          case 394:
          case 396:
          case 397:
          case 398:
          case 399:
          case 402:
          default:
            break;
          case 48:
            ToggleNoLayoutKey(26);
            break;
          case 50:
            ToggleNoLayoutKey(26);
            break;
          case 176:
          case 205:
            ToggleNoLayoutKey(85);
            break;
          case 177:
            ToggleNoLayoutKey(85);
            break;
          case 181:
            ToggleNoLayoutKey(87);
            break;
          case 182:
            ToggleNoLayoutKey(88);
            break;
          case 183:
            ToggleNoLayoutKey(86);
            break;
          case 233:
            ToggleNoLayoutKey(24);
            break;
          case 234:
            ToggleNoLayoutKey(25);
            break;
          case 226:
            ToggleNoLayoutKey(91);
          }
        }
      }
      this.EK_PrevBufKeys[j] = paramArrayOfInt1[j];
      this.EK_PrevBufUses[j] = paramArrayOfInt2[j];
    }
  }

  void UpdateKB(int paramInt1, int[] paramArrayOfInt, int paramInt2)
  {
    Singleton.getInstance().Debug("BlueInput::Action", "UpdateKB call state_keys=" + paramInt1 + " nKB=" + paramInt2);
    int i = 0;
    int i5 = 0;
    int j= 0;
     int i4= 0;
     int i2= 0;
    if (paramInt1 != this.OLDSK_ARRAY)
    {
      i5 = 0;
      if (i5 < 8);
    }
    else
    {
      this.OLDSK_ARRAY = paramInt1;
      j = 0;
      if (j < paramInt2)
//        break label419;
      if (this.KB_PrevBufSize >= i)
//        break label510;
      PlaySound(0);
      i4 = this.KB_PrevBufSize;
      if (i4 < i)
//        break label455;
      i2 = 0;
//      label100: if (i2 < this.KB_PrevBufKeys.length)
//        break label890;
    }
    for (int i3 = 0; ; i3++)
    {
      if (i3 >= paramInt2)
      {
        this.KB_PrevBufSize = i;
        Singleton.getInstance().Debug("BlueInput::Action", "UpdateKB done");
//        return;
        int i6 = 0;
        if ((paramInt1 & 1 << i5) != 0)
          i6 = 1;
        int i7 = 0;
        if ((this.OLDSK_ARRAY & 1 << i5) != 0)
          i7 = 1;
        int i8 = 0;
        boolean bool3 = false;
     String str = null;
        if (i6 != i7)
        {
          i8 = KBLayout.MODIFIER_KEY_CODES[i5];
          if (i6 <= i7)
//            break label399;
          bool3 = true;
          if ((i5 == 1) || (i5 == 5))
            this.g_bShifted = bool3;
          if ((i5 == 2) || (i5 == 6))
            this.g_bAlted = bool3;
          if ((i5 == 0) || (i5 == 4))
            this.g_bControled = bool3;
          Singleton localSingleton = Singleton.getInstance();
          StringBuilder localStringBuilder = new StringBuilder("MODIKEY ");
          if (!bool3)
//            break label405;
          str = "down";
//          label276:
        	  localSingleton.Debug("BlueInput::Action", str + " code=" + i8 + " index:" + i5 + " KBLayout:" + KBLayout.m_nLayoutID + "\n" + "shift:" + this.g_bShifted + " alt:" + this.g_bAlted + " contro:" + this.g_bControled);
//          if (KBLayout.m_nLayoutID == 0)
//            if (!bool3)
//              break label413;
        }
//        label399: label405: label413: 
        	for (int i9 = 0; ; i9 = 1)
        {
          SendNoLayoutKey(i8, i9);
          i5++;
//          break;
          bool3 = false;
//          break label195;
          str = "up";
//          break label276;
        }
      }
      if (paramArrayOfInt[i3] == 0)
        continue;
      this.KB_PrevBufKeys[i3] = paramArrayOfInt[i3];
    }
  }
}